<?php

/**
 * 控制器
 *
 * @author johnson
 * @package cip.modules.cacm.controllers
 */

namespace api\modules\openapi\controllers;

use api\modules\openapi\components\behaviors\BaseBehavior;
use api\traits\ControllerTrait;
use Yii;
use yii\filters\Cors;
use yii\web\Response;

class BaseController extends \yii\rest\Controller
{
    use ControllerTrait;

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors = array_merge(['cors' => [
            'class' => Cors::className(),
        ]], $behaviors);
        if (Yii::$app->getRequest()->getMethod() !== 'OPTIONS') {
            $this->attachBehavior('checkAuth', new BaseBehavior());
        }
        Yii::$app->response->format = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function beforeAction($action)
    {
        if (Yii::$app->getRequest()->getMethod() !== 'OPTIONS') {
            // BaseBehavior.php
            return $this->check();
        }
        return true;
    }
}